package com.spring.AOP;

import com.spring.AOP.AopInterface.ProceedJoinPoint;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class DefaultProceedJoinPoint implements ProceedJoinPoint {

    private final Object target;       // 目标对象（被代理的原始对象）
    private final Method method;       // 目标方法
    private final Object[] originalArgs; // 原始方法参数

    public DefaultProceedJoinPoint(Object target, Method method, Object[] originalArgs) {
        this.target = target;
        this.method = method;
        this.originalArgs = originalArgs != null ? originalArgs.clone() : new Object[0];
    }
    @Override
    public Object proceed() throws InvocationTargetException, IllegalAccessException {
        return method.invoke(target, originalArgs);
    }
    @Override
    public Object proceed(Object[] args) throws Throwable {
        return method.invoke(target, args);
    }
    @Override
    public Object getTarget() {
        return target;
    }

    @Override
    public Method getMethod() {
        return method;
    }

    @Override
    public Object[] getArgs() {
        // 返回原始参数的副本（保护性拷贝）
        return originalArgs.clone();
    }

    @Override
    public String toString() {
        return "ProceedingJoinPoint{" +
                "target=" + target.getClass().getSimpleName() +
                ", method=" + method.getName() +
                ", args=" + Arrays.toString(originalArgs) +
                '}';
    }
}
